var bmlcid = "";
var bgbm ="";
$(document).ready(function(){
	$(function(){
		$(".tab-pane").height(800)
		  .css("overflow-y","scroll")
		  .css("overflow-y","auto")
		  .css("overflow-x","hidden");
	})
	$("#functionNav").attr("href",navAddr);
	$("#xgbmBtn").hide();
	$("#xgfwBtn").hide();
	$("#zxBtn").hide();
	$("#lcBtn").hide();
	$("#dykpBtn").hide();
	$("#cpdaBtn").hide();
	getUserRight();
	//获取url中的参数
	var type =GetQueryString("type");
	var zdjh = $('#detailModal', parent.document).attr("zdjh");
	//点击返回按钮
	$("#backBtn").click(function(){		
		window.location.href= history.go(-1);
	})
		//获取藏品详细信息显示权限
	if(type!="view"){//是否是只读？只有总账查询打开页面才显示流程按钮		
//	ajaxFunc("post",false,"antiques/zzquery.getCangpinquanxian.do?zdjh="+zdjh,"",setRole,"");
		var role = ajaxResult("get",false,"antiques/zzquery.getCangpinquanxian.do?zdjh="+zdjh,"");
		var roleid = role.data.role;
		var bmbz = role.data.bmbz;
		var zxbz = role.data.zxbz;
		bgbm = role.data.bgbm;
		
		if(roleid=="1"&&zxbz=="0" && userInfo.orgid == bgbm){//库房管理员
			if(bmbz=="0"){
				$("#xgbmBtn").show();
				$("#xgfwBtn").show();
				$("#zxBtn").show();
				$("#dykpBtn").show();
			}else if(bmbz=="1"||bmbz=="2"){
				$("#dykpBtn").show();
				$("#lcBtn").show();
				$("#lcBtn").click(function(){
					//打开流程图链接
				})
			}
		}else if(roleid=="2"){
			$("#cpdaBtn").show();
//			$("#dykpBtn").show();
		}
	}
	
/*	//if(userInfo.roleid=="cangpinchaxunyuan"){
	  if(userInfo.roleid.indexOf("cangpinchaxunyuan"))
		$(".nav-tabs li").hide();
		$(".nav-tabs li:first").show();
		$("#tempIframe").attr("src","cpCard_simple.html?zdjh=" + escape(zdjh)+"&roleid="+roleid);		
	}else{
		//设置detail的iframe
		var mb = ajaxResult("get",false,"antiques/zzquery.getMbDetailByZdjh.do?zdjh="+zdjh,"");
		if(mb.status=="ok"&&mb.data.id!=null&&mb.data.id!=""&&mb.data.description!=""){
			$("#tempIframe").attr("src",mb.data.description+".html?zdjh=" + escape(zdjh)+"&roleid="+roleid);	 
		}else
			$("#tempIframe").attr("src","tongYong.html?zdjh=" + escape(zdjh)+"&roleid="+roleid);	
	}	*/
	
	//总账查询权限
	var zzcxRight = 0;
	if(userInfo.roleid.indexOf("zongzhangguanliyuan") != -1 || userInfo.roleid.indexOf("cangpinzhengjiyuan") !=-1 || userInfo.roleid.indexOf("guanjilingdao") != -1){
		zzcxRight = 1;
	}else if ((userInfo.roleid.indexOf("zhurenjilingdao") != -1 && userInfo.orgid=="bangongshi") || (userInfo.roleid.indexOf("fuzhurenjilingdao") != -1 && userInfo.orgid=="bangongshi")){
		zzcxRight = 1;
	}else if (userInfo.roleid.indexOf("zerenbaoguanyuan") != -1 && userInfo.orgid == bgbm){
		zzcxRight = 1;
	}	
	
	if(zzcxRight){
		//设置detail的iframe
			var mb = ajaxResult("get",false,"antiques/zzquery.getMbDetailByZdjh.do?zdjh="+zdjh,"");
			if(mb.status=="ok"&&mb.data.id!=null&&mb.data.id!=""&&mb.data.description!=""){
				$("#tempIframe").attr("src",mb.data.description+".html?zdjh=" + escape(zdjh)+"&roleid="+roleid);	 
			}else
				$("#tempIframe").attr("src","tongYong.html?zdjh=" + escape(zdjh)+"&roleid="+roleid);	
	}else{			 
		$(".nav-tabs li").hide();
		$(".nav-tabs li:first").show();
		$("#tempIframe").attr("src","cpCard_simple.html?zdjh=" + escape(zdjh)+"&roleid="+roleid);		
	}
	
	
	
	//设置完残情况iframe
	$("#tempIframe_wc").attr("src","zongZhangDetailWc.html?zdjh=" + escape(zdjh)+"&roleid="+roleid);	
  
	//初始化鉴定意见Table
	createTable('#identifytbl','antiques/zzquery.getJianDingXinXiList.do?zdjh='+zdjh,'zdjh',identifytblColumns,{'zdjh':zdjh});
  
	//初始化附件Table
	createTable('#attachmentstbl','antiques/zzquery.getXiangGuanZiLiaoList.do?zdjh='+zdjh,'zdjh',attachmentstblColumns,{'zdjh':zdjh});
 
	//初始化附属物Table
	createTable('#appendagestbl','antiques/zzquery.getFuShuWuList.do?zdjh='+zdjh,'id',appendagestblColumns,{'zdjh':zdjh});
  
	//初始化完残情况Table
	createTable('#woundstbl','antiques/zzquery.getWanCanQingKuangList.do?zdjh='+zdjh,'id',woundstblColumns,{'zdjh':zdjh});
  
	//初始化保养记录Table
	createTable('#repairtbl','antiques/zzquery.getXiuFuBaoYangList.do?zdjh='+zdjh,'id',repairtblColumns,{'zdjh':zdjh});
	
	//初始化出退库Table
	createTable('#jintui','antiques/zzquery.getDongTaiXinXiList.do?zdjh='+zdjh,'id',tblColTitle,{'zdjh':zdjh});

	//点击修改编目按钮
	$("#xgbmBtn").click(function(){
		window.parent.location.href=atomAddr+"/cpxgbm/create?zdjh="+zdjh;
	})
	
	//点击注销按钮
	$("#zxBtn").click(function(){
		window.parent.location.href=atomAddr+"/cpzx/create?zdjh="+zdjh;
	})
	
	//选择方位描述时，切换显示方位输入框
	 $("select[name=fwlx]").change(function(){
		 var fwlxid = $(this).val();
		 var fwlxmc = $("select[name=fwlx]").find("option:selected").text();;
		 if(fwlxid!=null && fwlxid!=""){
			 var fwlxArr = fwlxmc.split('-');
			 $(".fwxx").hide();
			 for(i=0;i<fwlxArr.length;i++){
				 $("label[name=fw"+(i+1)+"lbl]").text(fwlxArr[i]);
				 $("input[name=fw"+(i+1)+"],label[name=fw"+(i+1)+"lbl]").show();
			 }			 
		 }else{
			 $(".fwxx").hide();
		 }
		 $("#fwxxDiv input[name^=fw]").each(function(){
			 if($(this).css('display')=="none"){
				 $(this).val("");
			 }
		 });
		 $("#fwxxDiv label[name^=fw]").each(function(){
			 if($(this).css('display')=="none"){
				 $(this).val("");
			 }
		 });		 
	 });
	//隐藏方位信息输入
	$(".fwxx").hide();
	//获取并填充方位类型
	var fwmsJson = ajaxResult("get",false,ProjectName+"/sysmanage.getIdAndNameInXtjb.do",{'tableName':'xtjb_fwms'});
	loadSelect("#form_fw select[name=fwlx]",fwmsJson.rows,'id','name');
	
	//打开方位窗口
	$("#xgfwBtn").click(function(){
		 //$('#fwModal').modal('show'); 
		 result = ajaxResult("get",false,ProjectName+'/zzquery.getFw.do?zdjh='+zdjh,"");
	     if(result["status"] == 'ok'){	
		   loadData("#form_fw",result.data); 
		   if(result.data.fwxx!="" && result.data.fwxx!=null){
			   fwxx=result.data.fwxx.split("-");
			   for(i=0;i<fwxx.length;i++){
				   $("#form_fw input[name=fw"+(i+1)+"]").val(fwxx[i]);
			   }
		   }
		   $("select[name=fwlx]").change();
		   $('#fwModal').modal('show'); 
	     }else{
		   alertFunc(result["error"]);
	     }
	})
		
	//提交方位信息
	 $("#fw_btn").click(function(){
		var fw1=$('#form_fw input[name=fw1]').val();
		var fw1lbl=$("label[name=fw1lbl]").text();
		var fw2=$('#form_fw input[name=fw2]').val();
		var fw2lbl=$("label[name=fw2lbl]").text();
		var fw3=$('#form_fw input[name=fw3]').val();
		var fw3lbl=$("label[name=fw3lbl]").text();
		var fwlx =$('#form_fw #fwlx').children('option:selected').val();
		var fwms=$('#form_fw #fwms').val();
		var fwh=$('#form_fw #fwh').val();
		var fwxx='';	//方位信息
		var fw='';		//方位
		if(fw1=='' && fw2=='' && fw3==''){
			alertFunc('请输入方位信息');
		}else{
			if(fw1!=null && fw1!=''){
				fwxx += fw1;
				fw += fw1+fw1lbl;
			}				
			if(fw2!=null && fw2!=''){
				fwxx += '-'+fw2;
				fw += fw2+fw2lbl;
			}				
			if(fw3!=null && fw3!=''){
				fwxx += '-'+fw3;
				fw += fw3+fw3lbl;
			}		
			if(checkform("#form_fw")){
				var params =$("#form_fw").serializeObject();
				if(params.id != null || params.id != ""){
					   result = ajaxResult("get",false,ProjectName+'/zzquery.updateFw.do?zdjh='+zdjh+'&fwlx='+fwlx+'&fw='+fw+'&fwms='+fwms+'&fwxx='+fwxx+'&fwh='+fwh+"&userid="+userInfo.userid+"&username="+userInfo.username,'');
					   if(result["status"] == 'ok'){
						   alertFunc("录入方位信息成功！");
						   $('#fwModal').modal('hide');
						   //refreshTable('#dbTable','#form1');
						   $('#tempIframe').attr('src', $('#tempIframe').attr('src'));
					   }else{
						   alertFunc(result["error"]);
					   }
				   }
			}
		}
	 });
	
	
	$("#dykpBtn").click(function(){
		window.location.href=ProjectName+"/zzquery.exportExcelCp.do?zdjh="+zdjh+"&username="+userInfo.username;
	});

	$("#cpdaBtn").click(function(){
		window.location.href=ProjectName+"/zzquery.exportExcelCp_da.do?zdjh="+zdjh+"&username="+userInfo.username;
	});
}); 

//点击查看流程按钮	
function viewLc(){
	if(bmlcid!="")
		window.parent.location.href=atomAddr+bmlcid;
}

//影像table表头
var imgtblColumns=[{
    field: 'yxzt',
    title: '影像主体',
    formatter: function(value, row, index) {     	
    	if(value==1)	
      	    return "藏品";
    	else if(value==2)
    		return "完残";
    	else
    		return "";
      	}
}, {
    field: 'yxmc',
    title: '影像名称',
    formatter: function(value, row, index) {     	
    	if(value==1)	
      	    return "藏品";
    	else if(value==2)
    		return "完残";
    	else
    		return "";
      	}
}, {
    field: 'psfxms',
    title: '拍摄方向'
}, {
    field: 'psrmc',
    title: '拍摄人'
},{
    field: 'pssj',
    title: '拍摄时间'
}, {
    field: 'sfdb',
    title: '代表影像',
    formatter: function(value, row, index) {     	
    	if(value==1)	
      	    return "是";
    	else
    		return "";
      	}
}]

//鉴定意见table表头
var identifytblColumns=[
  {
    field: 'jdyy',
    title: '鉴定原因'
  },
  {
    field: 'jdr',
    title: '鉴定人'
  }, {
    field: 'jddw',
    title: '鉴定单位'
  }, {
    field: 'jdsj',
    title: '鉴定时间'
  }, {
    field: 'lrrmc',
    title: '操作人'
  }];

//附件table表头
var attachmentstblColumns=[{
    field: 'wjmc',
    title: '文件名'
}, {
    field: 'lrsj',
    title: '上传日期'
}, {
    field: 'lrrmc',
    title: '操作人'
},{
    field: '',
    title: '操作',
    formatter: function(value, row, index) {
    	return "<a onclick='window.open(\""+downloadAddr+row.wjdz+"\")' href='javascript:void(0)'>下载</a>";        	
     }
}]

//附属物table表头
var appendagestblColumns=[{
    field: 'fswbh',
    title: '附属物编号'
  }, {
    field: 'fswmc',
    title: '附属物名称'
  }, {
    field: 'fswsl',
    title: '附属物数量'
  }, {
    field: 'lrrmc',
    title: '操作人'
  }, {
    field: 'lrsj',
    title: '上传时间'
  }];

//完残情况table表头
var woundstblColumns=[{
    field: 'skzt',
    title: '完残主体'
}, {
    field: 'ssyy',
    title: '伤况原因'
}, {
    field: 'wcqk',
    title: '完残情况'
}, {
    field: 'bczt',
    title: '保存状态'
}, {
    field: 'sfxf',
    title: '是否需要修复'
}]

//修复保养记录table表头
var repairtblColumns=[{
    field: 'xfyy',
    title: '修复保养原因'
  }, {
    field: 'xfgc',
    title: '修复保养过程'
  }, {
    field: 'xfr',
    title: '修复保养人'
  }, {
    field: 'xfkssj',
    title: '开始时间'
  }, {
    field: 'xfjssj',
    title: '结束时间'
  }];
var tblColTitle = [{
	        field: 'dtlx',
	        title: '动态类型'
	    }, {
	        field: 'fssj',
	        title: '时间'
	    }, {
	        field: 'baoGuanYuan',
	        title: '库房责任保管员',
	        formatter: function(value, row, index) {
	        	var result =new Array();
	        	if(row.dtyy=="入库" || row.dtyy=="出库"){
	        		if(row.djrmc1!="")	
	    	      	    result.push(row.djrmc1);
	        		if(row.djrmc2!="")	
	    	      	    result.push(row.djrmc2);
	        	}else if(row.dtyy=="退库"){
	        		//没有返回点收人名称
	        	}else{
	        		//移库，显示那个字段的值
	        	}
	        	return result+"";
	         }
	        
	    }, {
	        field: 'tiYongRen',
	        title: '提用人',
	        formatter: function(value, row, index) {
	        	var result =new Array();
	        	if(row.dtyy=="出库"){
	        		if(row.djrmc1!="")	
	    	      	    result.push(row.tqrmc1);
	        		if(row.djrmc2!="")	
	    	      	    result.push(row.tqrmc2);
	        	}else if(row.dtyy=="退库"){
	        		if(row.djrmc1!="")	
	    	      	    result.push(row.shrmc1);
	        		if(row.djrmc2!="")	
	    	      	    result.push(row.shrmc2);
	        	}else{
	        		//移库 显示那个字段
	        	}
	        	return result+"";
	        }
	    }, {
	        field: 'jianDuYuan',
	        title: '监督员',
	        formatter: function(value, row, index) {
	        	var result ="";
	        	if(row.dtyy=="出库"){
	    	      	    result=row.ckjdrmc;
	        	}else if(row.dtyy=="退库"){
	        		result=row.tkjdrmc;
	        	}else{
	        		//移库 显示那个字段
	        	}
	        	return result+"";
	        }
	    }]